package com.banciyuan.bcywebview.biz.main.mineinfo.mark;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.CircleEnterStatistics;

/**
 * Created by squall on 2017/7/17.
 */

public class CurrentCircleSettingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CircleEnterStatistics> circleEnterStatisticses;
    private Context mContext;

    public CurrentCircleSettingAdapter(List<CircleEnterStatistics> circleEnterStatisticses, Context mContext) {
        this.circleEnterStatisticses = circleEnterStatisticses;
        this.mContext = mContext;
    }

    private class CurrentCircleHolder extends RecyclerView.ViewHolder {

        private ImageView iv_work;
        private TextView tv_work;

        public CurrentCircleHolder(View itemView) {
            super(itemView);
            iv_work = (ImageView) itemView.findViewById(R.id.iv_work);
            tv_work = (TextView) itemView.findViewById(R.id.tv_work);
        }
    }

    private class EmptyCircleHodler extends RecyclerView.ViewHolder {

        private ImageView iv_empty;
        private TextView tv_empty;
        private View parent;

        public EmptyCircleHodler(View itemView) {
            super(itemView);
            iv_empty = (ImageView) itemView.findViewById(R.id.iv_group_empty_pic);
            tv_empty = (TextView) itemView.findViewById(R.id.tv_group_desc);
            parent = itemView.findViewById(R.id.rl_group_empytview);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new EmptyCircleHodler(View.inflate(mContext, R.layout.circle_content_empty, null));
        } else {
            return new CurrentCircleHolder(View.inflate(mContext, R.layout.local_circle_deleteable_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position == 0) {
            EmptyCircleHodler e = (EmptyCircleHodler) holder;
            if (circleEnterStatisticses != null && circleEnterStatisticses.size() > 0) {
                e.parent.setVisibility(View.GONE);
            } else {
                e.parent.setVisibility(View.VISIBLE);
                e.iv_empty.setImageResource(R.drawable.bind_weibo);
                e.tv_empty.setText(mContext.getString(R.string.no_regular_circle));
            }
        } else {
            final CircleEnterStatistics c = circleEnterStatisticses.get(position - 1);
            final CurrentCircleHolder h = (CurrentCircleHolder) holder;
            ImageLoader.getInstance().displayImage(c.getCover(), h.iv_work, ImageOptions.options);
            if (!TextUtils.isEmpty(c.getName())) {
                h.tv_work.setText(c.getName());
            }
            h.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    circleEnterStatisticses.remove(c);
                    notifyItemRemoved(h.getAdapterPosition());
                    deleteCircleStatistics(c);
                }
            });
        }
    }

    private void deleteCircleStatistics(CircleEnterStatistics circleEnterStatistics) {
        final String compareKey = circleEnterStatistics.getType() + "#" + circleEnterStatistics.getId();
        new AsyncTask<Void, List<CircleEnterStatistics>, List<CircleEnterStatistics>>() {
            @Override
            protected List<CircleEnterStatistics> doInBackground(Void... params) {
                Gson gson = new Gson();
                String relakey = SPConstant.CIRCLESTATISTICS + UserDataHelper.getInstance(mContext).getUid();
                String data = SPHelper.getString(mContext, SPConstant.SPNAME_CIRCLESTATISTICS, relakey, "[]");
                List<CircleEnterStatistics> clist = gson.fromJson(data, new TypeToken<List<CircleEnterStatistics>>() {
                }.getType());
                List<CircleEnterStatistics> remainList = new ArrayList<>();
                for (CircleEnterStatistics c : clist) {
                    String key = c.getType() + "#" + c.getId();
                    if (!StringUtil.notNullEqual(key, compareKey)) {
                        remainList.add(c);
                    }
                }
                SPHelper.putString(mContext, SPConstant.SPNAME_CIRCLESTATISTICS, relakey, gson.toJson(remainList));
                return null;
            }

            @Override
            protected void onPostExecute(List<CircleEnterStatistics> circleEnterStatisticses) {
            }
        }.execute();
    }


    @Override
    public int getItemCount() {
        return circleEnterStatisticses.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }
}
